package cn.com.generaldata.os.ws.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

import cn.com.generaldata.os.constant.FilterConstant;
import cn.com.generaldata.os.paging.Filter;
import cn.com.generaldata.os.paging.FilterRule;
import cn.com.generaldata.os.service.common.MarkingService;
import cn.com.generaldata.os.vo.ReportMarkingVO;
import cn.com.generaldata.os.ws.intf.ReportMarkingWSInterface;

public class ReportMarkingWS implements ReportMarkingWSInterface {

	private MarkingService markingService;

	public MarkingService getMarkingService() {
		return markingService;
	}

	public void setMarkingService(MarkingService markingService) {
		this.markingService = markingService;
	}

	@Override
	public List<ReportMarkingVO> queryReportMarkings(String reportId) {
		Filter filter = new Filter();
		filter.setOrder(FilterConstant.ASC);
		filter.setSortBy("markingDate");

		if (StringUtils.isNotBlank(reportId)) {
			filter.addRule(new FilterRule(FilterConstant.LIKE, "report.id", reportId));
		}

		// return toVoList(markingService.queryReportMarking(filter, null));
		return null;
	}

	@Override
	public ReportMarkingVO queryReportMarkingById(String id) {
		return toVo(markingService.queryReportMarkingById(id));
	}

	private ReportMarkingVO toVo(cn.com.generaldata.os.pojo.ReportMarking pReportMarking) {
		if (pReportMarking != null) {

			String reportId = pReportMarking.getReport() == null ? null : pReportMarking.getReport().getId();

			String operatorId = pReportMarking.getOperator() == null ? null : pReportMarking.getOperator().getId();

			String operatorName = pReportMarking.getOperator() == null ? null : pReportMarking.getOperator().getUserLoginname();

			return new ReportMarkingVO(pReportMarking.getId(), reportId, operatorId, operatorName, pReportMarking.getReportStatus(),
					pReportMarking.getMarkingDate(), pReportMarking.getMarkingContent());
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	private List<ReportMarkingVO> toVoList(cn.com.generaldata.os.paging.Result pResult) {
		List<ReportMarkingVO> reportMarkings = new ArrayList<ReportMarkingVO>();
		for (cn.com.generaldata.os.pojo.ReportMarking pReportMarking : (List<cn.com.generaldata.os.pojo.ReportMarking>) pResult.getOperationResult()) {
			reportMarkings.add(toVo(pReportMarking));
		}
		return reportMarkings;
	}

}
